package com.taokatao.cardmee;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动生命控制器
 */
public class ActivityCollector {

    /**
     * 活动列表容器
     */
    public static final List<Activity> activities = new ArrayList<>();

    public static void addActivity(Activity activity) {
        if (!activities.contains(activity)) {
            activities.add(activity);
        }
    }

    public static void removeActivity(Activity activity) {
        activities.remove(activity);
    }

    public static void finishActivity(Class<?> activityClazz) {
        for (Activity activity : activities) {
            if (activityClazz.equals(activity.getClass())) {
                activity.finish();
            }
        }
    }

    public static int size() {
        return activities.size();
    }

    public static Activity obtainTop() {
        if (activities.size() == 0) return null;
        return activities.get(activities.size() - 1);
    }

    public static void finishAllExcept(Class<?> activityClazz) {
        for (Activity activity : activities) {
            if (!activity.isFinishing()) {
                if (activity.getClass().getSimpleName().equals(activityClazz.getSimpleName())) {
                    continue;
                }
                activity.finish();
            }
        }
    }

    public static void finishAll() {
        for (Activity activity : activities) {
            if (!activity.isFinishing()) activity.finish();
        }
    }

}
